//
// Created by tang on 23-4-23.
//

#include "meta_info.hpp"
#include "parser/utils.hpp"

MetaInfo::MetaInfo (const Cursor &cursor) {
    for (auto &child: cursor.children()) {
        if (child.kind() != CXCursor_AnnotateAttr)
            continue;
        for(auto &prop: extract_properties (child))
            properties_[prop.first] = prop.second;
    }
}

std::string MetaInfo::property (const std::string &key) const
{
    auto it = properties_.find (key);
    return it == end(properties_) ? "" : it->second;
}
bool MetaInfo::flag (const std::string &key) const
{
    return properties_.contains (key);
}
std::vector<MetaInfo::Property> MetaInfo::extract_properties (const Cursor &cursor)
{
    std::vector<Property> ret_list;

    auto prop_list = cursor.display_name();

    auto&& properties = split(prop_list, ";");

    static const std::string white_space_string = " \t\r\n";

    for (auto& property_item : properties)
    {
        auto&& item_details = split(property_item, ":");
        auto&& temp_string  = trim(item_details[0], white_space_string);
        if (temp_string.empty())
        {
            continue;
        }
        ret_list.emplace_back(temp_string,
                               item_details.size() > 1 ? trim(item_details[1], white_space_string) : "");
    }

    return ret_list;
}

// config keys
DEFINE_KEY (All);
DEFINE_KEY (Fields);
DEFINE_KEY (Methods);
DEFINE_KEY (Skipped);
DEFINE_KEY (Include);
DEFINE_KEY (Enable);
DEFINE_KEY (Disable);
DEFINE_KEY (WhiteListFields);
DEFINE_KEY (WhiteListMethods);
DEFINE_KEY (Key);
